package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.BaseDomain;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.util.LogicException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService{


    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        travelRepository.save(travel);
    }

    @Override
    public void delete(String id) {
        travelRepository.deleteById(id);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public Travel get(String id) {
        Travel travel = travelRepository.findById(id).orElse(null);
        UserInfo userInfo = userInfoService.get(travel.getUserId());
        travel.setAuthor(userInfo);
        return travel;
    }

    @Override
    public List<Travel> list() {

        return travelRepository.findAll();
    }

    @Override
    public Page<Travel> queryPageList(TravelQuery qo) {
        Query query = new Query();
        if(qo.getState() != null){
            query.addCriteria(Criteria.where("state").is(qo.getState()));
        }
        if(StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        if(qo.getDayType()>-1){
            TravelCondition travelCondition = TravelCondition.DAY_TYPE.get(qo.getDayType());
            query.addCriteria(Criteria.where("day").lte(travelCondition.getMax()).gte(travelCondition.getMin()));
        }
        if(qo.getPerExpendType()>-1){
            TravelCondition travelCondition = TravelCondition.PEREXPEND_TYPE.get(qo.getPerExpendType());
            query.addCriteria(Criteria.where("perExpend").lte(travelCondition.getMax()).gte(travelCondition.getMin()));
        }
        Pageable pageable= null;
        if(qo.getOrderType()==1){
            pageable= PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "releaseTime");
        }else if(qo.getOrderType()==2){
            pageable= PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "viewnum");
        }else{
            pageable= PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        }
        Page<Travel> page = DBHelper.query(template, Travel.class, query, pageable);
        for (Travel travel : page) {
            UserInfo userInfo = userInfoService.get(travel.getUserId());
            travel.setAuthor(userInfo);
        }
        return page;
    }

    @Override
    public void audit(String id, int state) {
        //判断游记是否存在
        Travel travel = this.get(id);
        if(travel == null){
            throw new LogicException("不存在当前游记");
        }
        //判断当前状态
        if(travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("当前状态异常");
        }

        //修改状态
        if(state == Travel.STATE_RELEASE){
            travel.setState(Travel.STATE_RELEASE);
            travel.setReleaseTime(new Date());
            travel.setLastUpdateTime(new Date());

        }else if(state == Travel.STATE_REJECT){
            travel.setState(Travel.STATE_REJECT);
            travel.setLastUpdateTime(new Date());
        }
        this.update(travel);
    }

    @Override
    public Travel getContentById(String id) {
        Travel travel = travelRepository.findById(id).get();
        return travel;
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());
        travel.setLastUpdateTime(new Date());
        if(StringUtils.hasLength(travel.getId())){
            travel.setReleaseTime(travel.getReleaseTime());
            this.update(travel);
        }else{
            travel.setCreateTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public List<Travel> queryByDestName(String keyword) {
        List<Travel> travelList = travelRepository.findByDestName(keyword);
        for (Travel travel : travelList) {
            UserInfo userInfo = userInfoService.get(travel.getUserId());
            travel.setAuthor(userInfo);
        }
        return travelList;
    }


}
